import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/common_models/user_model.dart';

/// 删除的 item widget
class DelUserItemWidget extends StatelessWidget {
  final UserModel model;
  final VoidCallback onDelete;

  const DelUserItemWidget({Key key, this.model, this.onDelete}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return UserItemWidget(
      model: this.model,
      suffix: BaseCloseButton(onPress: onDelete),
    );
  }
}

/// 选择状态的 item
class CheckBoxUserItemWidget extends StatefulWidget {
  final UserModel model;
  final bool isSelected;
  final Function(bool) onChange;

  const CheckBoxUserItemWidget({Key key, @required this.model, this.isSelected = false, this.onChange}) : super(key: key);

  @override
  _CheckBoxUserItemWidgetState createState() => _CheckBoxUserItemWidgetState();
}

class _CheckBoxUserItemWidgetState extends State<CheckBoxUserItemWidget> with AutomaticKeepAliveClientMixin {
  bool _selected;

  @override
  void initState() {
    _selected = widget.isSelected;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant CheckBoxUserItemWidget oldWidget) {
    if (_selected != widget.isSelected) {
      _selected = widget.isSelected;
    }
    if (oldWidget.isSelected != widget.isSelected) {
      _selected = widget.isSelected;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseCardComponent(
      onTap: () {
        _selected = !_selected;
        widget.onChange(_selected);
        setState(() {});
      },
      child: UserItemWidget(
        model: widget.model,
        suffix: BaseCustomCheckboxButton.tile(checked: _selected, disable: true),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

/// 通用 UserItemWidget
class UserItemWidget extends StatelessWidget {
  final UserModel model;
  final Widget suffix;
  final Widget expandWidget;

  const UserItemWidget({Key key, @required this.model, this.suffix, this.expandWidget}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (model?.id == null) return SizedBox();
    return IntrinsicHeight(
      child: Row(
        children: [
          Align(
            alignment: Alignment.topLeft,
            child: BaseImageComponent(
              imageUrl: this.model.img,
              placeholderName: model.name,
              type: BaseImageTypeEnum.X3User,
              isClipOval: true,
              width: 40,
              height: 40,
            ),
          ),
          SizedBox(width: 10),
          Expanded(child: _buildContent(context)),
          SizedBox(width: 8),
          this.suffix ?? SizedBox(),
        ],
      ),
    );
  }

  /// 创建详情
  Widget _buildContent(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        DefaultTextStyle(
          style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 14),
          child: Row(
            children: [
              Flexible(child: Text(this.model.userName)),
              SizedBox(width: 4),
              Text("(${this.model.userId})", maxLines: 1, overflow: TextOverflow.ellipsis),
            ],
          ),
        ),
        this.expandWidget ?? SizedBox(),
      ],
    );
  }
}
